<!-- 
	dao.php
	desc: Contains all Data Access Objects used by CMS
-->

<?php
include 'dbconnector.php';

class User{
	private $id;
	private $name;
	private $username;
	private $password;
	private $activated;
	private $is_admin;

	function User($i_id, $i_name, $i_username, $i_password, $i_activated, $i_is_admin){
		$this->id = $i_id;
		$this->name = $i_name;		
		$this->username = $i_username;
		$this->password = $i_password;
		$this->activated = $i_activated;
		$this->is_admin = $i_is_admin;
	}
	
	// getters
	function getId(){
		echo $this->id;
	}
	function getName(){
		echo $this->name;
	}	
	function getUsername(){
		echo $this->username;
	}			
	function getPassword(){
		echo $this->password;
	}			
	function getActivated(){
		echo $this->activated;
	}				
	function getIs_Admin(){
		echo $this->is_admin;
	}						

}

class Entry{
	private $id;
	private $time;
	private $content;
	private $remarks;
	private $person;
	private $status;
	
	function Entry($i_id, $time, $i_content, $i_remarks, $i_person, $i_status){	
		$this->id =$i_id;
		$this->time =$i_time;
		$this->content =$i_content;
		$this->remarks =$i_remarks;
		$this->person =$i_person;
		$this->status =$i_status;
	}
	
	// getters
	function getId(){
		return $this->id;
	}	
	function getTime(){
		return $this->time;
	}
	function getContent(){
		return $this->content;
	}
	function getRemarks(){
		return $this->remarks;
	}
	function getPerson(){
		return $this->person;
	}		
	function getStatus(){
		return $this->status;
	}		
	
}

// contains methods used to access the USER table
//   - addUser
//	 - checkPassword
//	 - modifyUser
//   - isExistingUser
//   - setActive
//   - setInactive
//   - retrieveAllUsers
class UserPeer{	

	// adds a new row on the user table
	function addUser($name, $username, $password){
		if($username!="" AND $password!="" AND $name!=""){			
			//-- connect to mysql then select database
			$mySQLLink = new MySQLLink;
			$mySQLLink->connect();
			@mysql_select_db($mySQLLink->database) or die("Unable to select database");
			//--
			
			//create then execute query
			$query_add_user = "INSERT INTO user	(name,username,password) VALUES('".$name."','".$username."',md5('".$password."'));";
			mysql_query($query_add_user);
			
			//commit then close
			mysql_query("COMMIT");
			$mySQLLink->disconnect();
			
			return true;
		}else{
			return false;
		}	
	}	
	
	// checks if a submitted password matches the correct one for the submitted username
	function checkPassword($i_username,$i_password){
		//-- connect to mysql then select database
		$mySQLLink = new MySQLLink;
		$mySQLLink->connect();
		@mysql_select_db($mySQLLink->database) or die("Unable to select database");
		//--
		
		// create then execute query			
		$query_get_user_password = "SELECT * FROM user WHERE username='".$i_username."';";
		$r_password = mysql_result(mysql_query($query_get_user_password),0,"password");
		
		// compare md5 code for the submitted password to the md5 of the correct one
		if ($r_password == md5($i_password)){
			return true;
		}else{
			return false;
		}
		
		//close
		$mySQLLink->disconnect();
		return true;		
	}
	
	// modifies the name and password of specified username
	function modifyUser($i_username,$i_password,$_name){
		//-- connect to mysql then select database
		
		//check if username exists
		if(!$this->isExistingUser($i_username))return false;
		
		$mySQLLink = new MySQLLink;
		$mySQLLink->connect();
		@mysql_select_db($mySQLLink->database) or die("Unable to select database");
		//--		
		
		// create then execute query			
		$query_modify_user = "UPDATE user SET password=md5('".$i_password."'), name='".$_name."'WHERE username='".$i_username."';";
		mysql_query($query_modify_user);
		
		//-- commit then close
		mysql_query("COMMIT");
		$mySQLLink->disconnect();
		//--
		return true;	
	}
	
	// checks if a username is already in use
	function isExistingUser($i_username){
	
		//-- connect to mysql then select database
		$mySQLLink = new MySQLLink;
		$mySQLLink->connect();
		@mysql_select_db($mySQLLink->database) or die("Unable to select database");
		//--		
		
		// create then execute query	
		$query_users = "SELECT * FROM user WHERE username='".$i_username."';";
		$rows = mysql_num_rows(mysql_query($query_users));
		
		if($rows==0){
			return false;		
		}else{
			return true;
		}
		
		//-- close
		$mySQLLink->disconnect();
		//--		
	}
	
	// activates a user account
	function setActive($i_username){		
		// check if username exists
		if(!$this->isExistingUser($i_username))return false;
		
		//-- connect to mysql then select database
		$mySQLLink = new MySQLLink;
		$mySQLLink->connect();
		@mysql_select_db($mySQLLink->database) or die("Unable to select database");
		//--		
		
		// create then execute query			
		$query_modify_user = "UPDATE user SET activated='1';";
		mysql_query($query_modify_user);
		
		//-- commit then close
		mysql_query("COMMIT");
		$mySQLLink->disconnect();
		//--
		return true;			
	}
	
	// deactivates a user account
	function setInactive($i_username){		
		// check if username exists
		if(!$this->isExistingUser($i_username))return false;
		
		//-- connect to mysql then select database
		$mySQLLink = new MySQLLink;
		$mySQLLink->connect();
		@mysql_select_db($mySQLLink->database) or die("Unable to select database");
		//--		
		
		// create then execute query			
		$query_modify_user = "UPDATE user SET activated='0';";
		mysql_query($query_modify_user);
		
		//-- commit then close
		mysql_query("COMMIT");
		$mySQLLink->disconnect();
		//--
		return true;			
	}
		
	function retrieveAllUsers(){
		$allUsers = array();
		
		//-- connect to mysql then select database
		$mySQLLink = new MySQLLink;
		$mySQLLink->connect();
		@mysql_select_db($mySQLLink->database) or die("Unable to select database");
		//--		
		
		// create then execute query			
		$query_retrieve_users = "SELECT * FROM user";		
		
		$result=mysql_query($query_retrieve_users);
		$count=mysql_numrows($result);
		
		for($i=0;$i<$count;$i++){
			$o_id = mysql_result($result,$i,"id");
			$o_name = mysql_result($result,$i,"name");
			$o_username = mysql_result($result,$i,"username");
			$o_password = mysql_result($result,$i,"password");
			$o_activated = mysql_result($result,$i,"activated");
			$o_is_admin = mysql_result($result,$i,"is_admin");
			
			$user = new User($o_id,$o_name,$o_username,$o_password,$o_activated,$o_is_admin);
			$allUsers[$i] = $user;
		}
		
		
		//-- commit then close
		mysql_query("COMMIT");
		$mySQLLink->disconnect();
		//--
		return $allUsers;		
	}
} // end of UserPeer


// contains menthod used to access the ENTRY table

class EntryPeer{

	function addEntry($i_time,$i_content,$i_remarks,$i_person,$i_status){
		if($i_time!="" AND $i_content!="" AND $i_person!="" AND $i_status){			
			//-- connect to mysql then select database
			$mySQLLink = new MySQLLink;
			$mySQLLink->connect();
			@mysql_select_db($mySQLLink->database) or die("Unable to select database");
			//--
			
			//mysql format: 'YYYY-MM-DD HH:MM:SS'
			//for php date use: date("Y-m-d H:i:s");
			
			//create then execute query
			$query_add_entry = "INSERT INTO entry(time,content,remarks,person,status) VALUES('".$i_time."','".$i_content."','".$i_remarks."','".$i_person."','".$i_status."');";
			mysql_query($query_add_entry);
			
			//commit then close
			mysql_query("COMMIT");
			$mySQLLink->disconnect();
			
			return true;
		}else{
			return false;
		}			
		
	}
}

	
?>
